/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.spring;

import io.gitee.huangjianbo.supos.openapi.core.SuposApiClient;
import io.gitee.huangjianbo.supos.openapi.core.SuposApiClientFactory;
import io.gitee.huangjianbo.supos.openapi.core.config.SuposApiClientConfig;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpLanguage;
import io.gitee.huangjianbo.supos.openapi.httpclient5.httpclient.HttpClient5Config;
import io.gitee.huangjianbo.supos.openapi.httpclient5.httpclient.HttpClient5Manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tomcat
 * @version 1.0
 */
@Configuration
@EnableConfigurationProperties(value = {
        SuposApiClientConfigProperties.class,
        SuposApiClientHttpClient5ConfigProperties.class
})
@ConditionalOnProperty(prefix = "supos", name = "sdk")
public class SuposApiClientAutoConfiguration {

    @Autowired
    private SuposApiClientConfigProperties clientConfigProperties;
    @Autowired
    private SuposApiClientHttpClient5ConfigProperties httpClient5ConfigProperties;

    @Bean
    @ConditionalOnMissingBean
    public SuposApiClient suposApiClient(SuposApiClientConfigProperties clientConfigProperties,
                                         SuposApiClientHttpClient5ConfigProperties httpClient5ConfigProperties) {
        HttpClient5Config httpClient5Config = HttpClient5Config.createDefault()
                .maxIdleTime(httpClient5ConfigProperties.getMaxIdleTime())
                .connectionTimeout(httpClient5ConfigProperties.getConnectionTimeout())
                .responseTimeout(httpClient5ConfigProperties.getResponseTimeout())
                .socketTimeout(httpClient5ConfigProperties.getSocketTimeout())
                .sndBufSize(httpClient5ConfigProperties.getSndBufSize())
                .rcvBufSize(httpClient5ConfigProperties.getRcvBufSize());
        HttpClient5Manager.setHttpClientConfig(httpClient5Config);

        SuposApiClientConfig clientConfig = SuposApiClientConfig.of()
                .ticket(clientConfigProperties.getTicket())
                .accessKey(clientConfigProperties.getAccessKey())
                .secretKey(clientConfigProperties.getSecretKey())
                .endpoint(clientConfigProperties.getEndpoint())
                .tenantId(clientConfigProperties.getTenantId())
                .appId(clientConfigProperties.getAppId())
                .serviceName(clientConfigProperties.getServiceName())
                .defaultLanguage(HttpLanguage.valueOf(clientConfigProperties.getDefaultLanguage()));

        return SuposApiClientFactory.getClient(clientConfig);
    }

    public void setClientConfigProperties(SuposApiClientConfigProperties clientConfigProperties) {
        this.clientConfigProperties = clientConfigProperties;
    }

    public SuposApiClientConfigProperties getClientConfigProperties() {
        return clientConfigProperties;
    }

    public void setHttpClient5ConfigProperties(SuposApiClientHttpClient5ConfigProperties httpClient5ConfigProperties) {
        this.httpClient5ConfigProperties = httpClient5ConfigProperties;
    }

    public SuposApiClientHttpClient5ConfigProperties getHttpClient5ConfigProperties() {
        return httpClient5ConfigProperties;
    }
}
